home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Winter Shell 1.0d2 / Source / Libraries / ControlLib / ControlLib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-13  |  7.3 KB  |  325 lines  |  [TEXT/KAHL]

  1. /* Functions for doing things to controls. At the moment it's not
  2.     very fancy, but features will be added as needed.
  3.     
  4.     Revision History:
  5.     
  6.     93/11/17 aih
  7.     - added function to invalidate control
  8.     
  9.     93/11/03 aih
  10.     - updated validation function
  11.     
  12.     93/10/18 aih
  13.     - added functions for creating and initializing a control
  14.     
  15.     92/02/20 AIH
  16.     - Added pattern parameter to CtlDefaultFrame
  17.     
  18.     91/06/13 AIH
  19.     - Time library is used to delay so that background tasks get time
  20.     
  21.     91/05/31 AIH
  22.     - Added a comment explaining why a region is calculated for the default
  23.     button's outline.
  24.     
  25.     91/04/17 AIH
  26.     - Modified function for framing the default button and added a function
  27.     to erase the frame
  28.     
  29.     91/04/10 AIH
  30.     - Added functions for accessing elements of a control handle
  31.     
  32.     91/04/07 AIH
  33.     - Enhanced control validation function
  34.     
  35.     91/03/11 AIH
  36.     - Added constant for width of a scroll bar
  37.     
  38.     91/03/03-05 AIH
  39.     - Added constants for my button CDEF
  40.     - A control is only reported as disabled if its hilite is CTL_DISABLED
  41.     
  42.     91/01/22 AIH
  43.     - Added a couple of functions
  44.     
  45.     91/01/21 AIH
  46.     - Added comment describing purpose of this file
  47.     
  48.     91/01/05 Ari Halberstadt (AIH)
  49.     - Inserted this standard header in all files */
  50.  
  51. #include <string.h>
  52. #include "pstr.h"
  53. #include "ControlLib.h"
  54. #include "DrawLib.h"
  55. #include "MemoryLib.h"
  56. #include "RectangleLib.h"
  57. #include "StringLib.h"
  58. #include "TimeLib.h"
  59. #include "WindowLib.h"
  60.  
  61. /* Hilite values for enabled/disabled control. To check for a disabled or
  62.     enabled control, compare the control's hilite value to CTL_DISABLED.
  63.     Don't compare it to CTL_ENABLED since the control could be enabled
  64.     with a part code, such as inButton for a standard push button. Better
  65.     yet, just use the appropriate functions from this library. */
  66. #define CTL_ENABLED        (0)
  67. #define CTL_DISABLED        (255)
  68.  
  69. /* true if control is a valid control */
  70. Boolean CtlValid(ControlHandle ctl)
  71. {
  72.     ControlHandle next = NULL;
  73.     Rect contrlRect;
  74.     
  75.     if (! HandleValidSize(ctl, sizeof(ControlRecord)-sizeof(Str255)-1)) return(false);
  76.     if (! WinValid((**ctl).contrlOwner)) return(false);
  77.     contrlRect = (**ctl).contrlRect;
  78.     if (! RectValid(&contrlRect)) return(false);
  79.     /* the control must be in its owner's window list */
  80.     next = ((WindowPeek) (**ctl).contrlOwner)->controlList;
  81.     while (next && next != ctl)
  82.         next = (**next).nextControl;
  83.     if (! next) return(false);
  84.     return(true);
  85. }
  86.  
  87. /* return the window containing the control */
  88. WindowPtr CtlWindow(ControlHandle ctl)
  89. {
  90.     require(CtlValid(ctl));
  91.     return((**ctl).contrlOwner);
  92. }
  93.  
  94. /* return the control's rectangle */
  95. void CtlRect(ControlHandle ctl, Rect *contrlRect)
  96. {
  97.     require(CtlValid(ctl));
  98.     *contrlRect = (**ctl).contrlRect;
  99.     ensure(RectValid(contrlRect));
  100. }
  101.  
  102. /* true if control is visible */
  103. Boolean CtlVisible(ControlHandle ctl)
  104. {
  105.     require(CtlValid(ctl));
  106.     return((**ctl).contrlVis != 0);
  107. }
  108.  
  109. /* return control's hilite */
  110. Byte CtlHilite(ControlHandle ctl)
  111. {
  112.     require(CtlValid(ctl));
  113.     return((**ctl).contrlHilite);
  114. }
  115.  
  116. /* invalidate the control */
  117. void CtlInval(ControlHandle ctl)
  118. {
  119.     Rect r;
  120.     GrafPtr port = NULL;
  121.     
  122.     GetPort(&port);
  123.     SetPort(CtlWindow(ctl));
  124.     CtlRect(ctl, &r);
  125.     InvalRect(&r);
  126.     SetPort(port);
  127. }
  128.  
  129. /* return next control */
  130. ControlHandle CtlNext(ControlHandle ctl)
  131. {
  132.     require(CtlValid(ctl));
  133.     return((**ctl).nextControl);
  134. }
  135.  
  136. /* return control's value */
  137. short CtlValue(ControlHandle ctl)
  138. {
  139.     require(CtlValid(ctl));
  140.     return(GetCtlValue(ctl));
  141. }
  142.  
  143. /* set control's value */
  144. void CtlValueSet(ControlHandle ctl, short value)
  145. {
  146.     require(CtlValid(ctl));
  147.     SetCtlValue(ctl, value);
  148. }
  149.  
  150. /* enable or disable the control */
  151. void CtlEnableSet(ControlHandle ctl, Boolean enable)
  152. {
  153.     require(CtlValid(ctl));
  154.     HiliteControl(ctl, enable ? CTL_ENABLED : CTL_DISABLED);
  155. }
  156.  
  157. /* true if the control is enabled */
  158. Boolean CtlEnabled(ControlHandle ctl)
  159. {
  160.     require(CtlValid(ctl));
  161.     return((**ctl).contrlHilite != CTL_DISABLED);
  162. }
  163.  
  164. /* get title of control */
  165. void CtlTitle(ControlHandle ctl, CStr255 title)
  166. {
  167.     require(CtlValid(ctl));
  168.     require(StrValid(title, -1));
  169.     GetCTitle(ctl, (StringPtr) title);
  170.     p2cstr((StringPtr) title);
  171. }
  172.  
  173. /* set title of control */
  174. void CtlTitleSet(ControlHandle ctl, const CStr255 title)
  175. {
  176.     Str255 pstr;
  177.     
  178.     require(CtlValid(ctl));
  179.     require(StrValid(title, sizeof(CStr255)));
  180.     SetCTitle(ctl, c2pstrcpy(pstr, title));
  181. }
  182.  
  183. /* show or hide the control */
  184. static void CtlShowHide(ControlHandle ctl, Boolean show)
  185. {
  186.     require(CtlValid(ctl));
  187.     if (show)
  188.         ShowControl(ctl);
  189.     else
  190.         HideControl(ctl);
  191. }
  192.  
  193. /* Define the region containing the default control's outline. The advantage
  194.     of defining a region is that EraseRgn can be used to erase the control's
  195.     outline. The advantage of using EraseRgn is that it will work regardless
  196.     of the background pattern and color of the window. To draw the outline
  197.     a call to PaintRgn suffices. This method contrasts with that recommended
  198.     in IM-I, p407.  */
  199. static void CtlDefaultRgn(ControlHandle ctl, RgnHandle frameRgn)
  200. {
  201.     Rect        frame;    /* rectangle to frame */
  202.     PenState    pen;        /* saved pen */
  203.     GrafPtr    port;        /* saved port */
  204.  
  205.     require(CtlValid(ctl));
  206.     require(ValidateRgn(frameRgn));
  207.     GetPort(&port);
  208.     SetPort(CtlWindow(ctl));
  209.     GetPenState(&pen);
  210.     PenNormal();
  211.     CtlRect(ctl, &frame);
  212.     check(! thePort->rgnSave);
  213.     OpenRgn();
  214.     InsetRect(&frame, -1, -1);
  215.     FrameRoundRect(&frame, 10, 10);
  216.     InsetRect(&frame, -3, -3);
  217.     FrameRoundRect(&frame, 16, 16);
  218.     CloseRgn(frameRgn);
  219.     SetPenState(&pen);
  220.     SetPort(port);
  221. }
  222.  
  223. /* Draw the outline around the default button. The ouline is drawn in
  224.     black if the control is enabled and its window is frontmost, otherwise
  225.     the outline is drawn in gray. If 'pat' isn't NULL the outline is
  226.     drawn in the pattern. */
  227. void CtlDefaultFrame(ControlHandle ctl, Pattern pat)
  228. {
  229.     RgnHandle frameRgn = NULL;    /* region containing frame */    
  230.     GrafPtr port = NULL;            /* saved port */
  231.     PenState pen;                    /* saved pen state */
  232.  
  233.     require(CtlValid(ctl));
  234.  
  235.     /* create region to contain frame */
  236.     frameRgn = BeginRgn();
  237.     
  238.     /* setup port and pen */
  239.     GetPort(&port);
  240.     SetPort(CtlWindow(ctl));
  241.     GetPenState(&pen);
  242.     PenNormal();
  243.     
  244.     /* define region containing frame */
  245.     CtlDefaultRgn(ctl, frameRgn);
  246.     
  247.     /* use gray if control is inactive */
  248.     if (! CtlEnabled(ctl))
  249.         PenPat(gray);
  250.     
  251.     /* draw frame */
  252.     if (pat)
  253.         PenPat(pat);
  254.     PaintRgn(frameRgn);
  255.     
  256.     EndRgn(frameRgn);
  257.     SetPenState(&pen);
  258.     SetPort(port);
  259. }
  260.  
  261. /* erase the default button's frame */
  262. void CtlDefaultErase(ControlHandle ctl)
  263. {
  264.     RgnHandle frameRgn = NULL;
  265.     GrafPtr port = NULL;
  266.     
  267.     require(CtlValid(ctl));
  268.     GetPort(&port);
  269.     SetPort(CtlWindow(ctl));
  270.     frameRgn = BeginRgn();
  271.     CtlDefaultRgn(ctl, frameRgn);
  272.     EraseRgn(frameRgn);
  273.     InvalRgn(frameRgn);
  274.     EndRgn(frameRgn);
  275.     SetPort(port);
  276. }
  277.  
  278. /* flash the button */
  279. void CtlFlashButton(ControlHandle ctl)
  280. {
  281.     short hilite;
  282.     
  283.     require(CtlValid(ctl));
  284.  
  285.     if (CtlEnabled(ctl)) {
  286.         hilite = (**ctl).contrlHilite;
  287.         HiliteControl(ctl, inButton);
  288.         TimeDelay(8);
  289.         HiliteControl(ctl, hilite);
  290.     }
  291. }
  292.  
  293. /* event handling stuff */
  294.  
  295. #include "EventLib.h"
  296.  
  297. /* true if the point is within the control */
  298. Boolean CtlWithin(ControlHandle ctl, Point where)
  299. {
  300.     require(CtlValid(ctl));
  301.     return(TestControl(ctl, where) != 0);
  302. }
  303.  
  304. short CtlMouseDown(ControlHandle ctl, EventRecord *event)
  305. {
  306.     Point where;
  307.     
  308.     require(CtlValid(ctl));
  309.     where = event->where;
  310.     GlobalToPort(&where, CtlWindow(ctl));
  311.     return(TrackControl(ctl, where, (ProcPtr) -1));
  312. }
  313.  
  314. void CtlInitialize(ControlHandle ctl)
  315. {
  316.     require(CtlValid(ctl));
  317.     WinRegister(CtlWindow(ctl), ctl, CtlEventTable());
  318. }
  319.  
  320. void CtlUninitialize(ControlHandle ctl)
  321. {
  322.     require(CtlValid(ctl));
  323.     WinUnregister(CtlWindow(ctl), ctl);
  324. }    
  325.